#!/bin/bash -e

SELFDIR=$(dirname "$(readlink -f "$0")")
SCRIPT=${SCRIPT:=./run}

SERVICES="\
  mender-inventory \
  mender-useradm \
  mender-device-auth \
  mender-deployments \
  mender-create-artifact-worker \
  mender-workflows-server \
  mender-workflows-worker \
  mender-deviceconnect \
  mender-deviceconfig \
"
DB_SERVICES="\
  mender-mongo
"

# stop service that are using DBs
# shellcheck disable=SC2086
${SCRIPT} stop ${SERVICES}

# make sure that DB services are up
# shellcheck disable=SC2086
${SCRIPT} start ${DB_SERVICES}

# dump DB
# shellcheck disable=SC2016
DUMP_PATH=$PWD ${SCRIPT} -f "${SELFDIR}/migration-helper.yml" \
         run \
         --rm \
         -e DB_SERVICES="${DB_SERVICES}" \
         mongo-helper \
         sh -c 'for s in ${DB_SERVICES}; do mongodump -h "$s" --out "/srv/db-dump/$s"; done'
